<?php
namespace Api\Controller;
use Think\Controller;
class WorkController extends BaseController {

    /**
     *    小程序获取所有的客片
     */
    public function index()
    {
        //查询出所有的城市
        $destination = M('destination')->where()->field('id,name,image')->select();

        //查询出所有的数据
        $work = M('works')->where('is_show = 1')->field('id,image,name,u_like')->order('create_at desc')->select();
        echo json_encode(
            array(
                'work' => $work,
                'de'=>$destination,
            )
        );
    }


    /**
     *   获取单个目的地下的所有的作品
     */
    public function getCaWorks(){
        $id = $_REQUEST['id'];
        if($id > 0){
            $where['id'] = $id;
        }

        $a['is_show'] = 1;
        if($id > 0){
            $a['d_id'] = $id;
        }


        $destination = M('destination')->where()->field('id,name,image')->select();
        $image = M('destination')->where($where)->field('image')->find();
        $list = M('works')->where($a)->field('id,image,name,is_view')->select();
        echo json_encode(
            array(
                'work' => $list,
                'de_list'=>$destination,
                'image'=>$image,
            )
        );
    }


    /**
     *   查看单个作品的详细内容
     */
    public function showWorkDetail(){
        $id = $_REQUEST['id'];
        $openid = $_REQUEST['openid'];

        //将浏览的图片加入到我的浏览足迹
        if($openid){
            $data['wid'] = $id;
            $data['uid'] = $openid;
            $aList = M('w_like')->where($data)->select();
            M('works')->where("id = '$id'")->setInc('is_view',3);
            if(!$aList){
                M('w_like')->add($data);
            }
        }

        $detail = M('works')->where("id = '$id'")->find();
        $a = $detail['in_images'];
        $b = $this->icon_to_utf8($a);
        $aImage = json_decode($b);
        echo json_encode(
            array(
                'work' => $detail,
                'aImage' => $aImage,
            )
        );
    }




    /**
     *   查看单个客片的详细内容
     */
    public function showWorkDetailKe(){
        $id = $_REQUEST['id'];
        $detail = M('sample')->where("id = '$id'")->find();
        $a = $detail['in_images'];
        $b = $this->icon_to_utf8($a);
        $aImage = json_decode($b);
        echo json_encode(
            array(
                'work' => $detail,
                'aImage' => $aImage,
            )
        );
    }




    /**
     *  字符串GBK转码为UTF-8，数字转换为数字。
     */
    function ct2($s){
        if(is_numeric($s)) {
            return intval($s);
        } else {
            return iconv("GBK","UTF-8",$s);
        }
    }


    /**
        批量处理gbk->utf-8
    */
    function icon_to_utf8($s) {
        if(is_array($s)) {
            foreach($s as $key => $val) {
                $s[$key] = $this->icon_to_utf8($val);
            }
        } else {
            $s = $this->ct2($s);
        }
        return $s;
    }

    /**
     *   用户给作品点赞功能
     */
    public function likeWorks(){
        $id = $_POST['id'];
        $uid = $_POST['uid'];
        //先查询是否有点赞过
        $list = M('w_like')->where("wid= '$id' and uid = '$uid'")->find();
        if($list){
            if($list['is_like'] == 0){
                $a_list = M('w_like')->where("wid= '$id' and uid = '$uid'")->setfield('is_like',1);
                M('works')->where("wid= '$id' ")->setInc('u_like');
                if($a_list){
                    echo json_encode(array('status'=>21,'err'=>'点赞成功！'));
                }else{
                    echo json_encode(array('status'=>404,'err'=>'点赞失败！'));
                }
            }elseif($list['is_like'] == 1){
                $a_list = M('w_like')->where("wid= '$id' and uid = '$uid'")->setfield('is_like',0);
                M('works')->where("wid= '$id' ")->setDec('u_like');
                if($a_list){
                    echo json_encode(array('status'=>21,'err'=>'取消点赞成功！'));
                }else{
                    echo json_encode(array('status'=>404,'err'=>'点赞失败！'));
                }
            }
        }else{
            $data['wid'] = $id;
            $data['uid'] = $uid;
            $a_list = M('w_like')->add($data);
            M('works')->where("wid= '$id' ")->setInc('u_like');
            if($a_list){
                echo json_encode(array('status'=>21,'err'=>'点赞成功！'));
            }else{
                echo json_encode(array('status'=>404,'err'=>'点赞失败！'));
            }
        }

    }






}
